package com.sense.kukoo.common.repo.kv.playlist

import com.google.gson.Gson
import com.sense.kukoo.common.media.bean.LocalMediaListItemBean
import com.sense.kukoo.common.repo.bean.PlayListBean
import com.sense.kukoo.common.repo.kv.KukooKeyValueManager
import com.sense.kukoo.lib_logger.LoggerManager
import com.tencent.mmkv.MMKV

class PlayListImpl(val mmkv: MMKV){

    private val TAG: String = javaClass.simpleName

    fun add(item: LocalMediaListItemBean):Int{
        val mPlayListBean:PlayListBean = getBean()

        //如果是有声书，同一合集看作同一歌曲，新章节覆盖旧章节；
        //其它情况看作不同歌曲
        //播放歌曲，全部放在首位
        var position = 0
        val id  = item.idOfChapter()
        val typeOfChapter = true

        if (mPlayListBean.mIds.contains(id)) {
            //已在列表
            mPlayListBean.mData.forEachIndexed { index, localMediaListItemBean ->
                var itemId = -1L
                if (typeOfChapter) {
                    itemId = localMediaListItemBean.idOfChapter()
                }else{
                    itemId = localMediaListItemBean.idOfBooks()
                }
                if(id == itemId){
                    position = index
                }
            }
            //更新数据
            mPlayListBean.mData.removeAt(position)
            mPlayListBean.mData.add(0, item)
        }else{
            //未在列表
            mPlayListBean.mData.add(0, item)
            mPlayListBean.mIds.add(id)
        }
        //
        saveBean(mPlayListBean)
        //
        return 0
    }

    fun delete(item: LocalMediaListItemBean):Int{
        val mPlayListBean:PlayListBean = getBean()

        val id = item.idOfChapter()

        if (mPlayListBean.mIds.contains(id)) {
            //已在列表
            var position = -1
            mPlayListBean.mData.forEachIndexed { index, localMediaListItemBean ->
                if(localMediaListItemBean.idOfChapter() == item.idOfChapter()){
                    position = index
                }
            }
            //删除数据
            mPlayListBean.mIds.remove(id)
            mPlayListBean.mData.removeAt(position)
            //
            saveBean(mPlayListBean)
            return position
        }else{
            //未在列表
            return -1
        }
    }

    fun deleteAll(){
        val mPlayListBean:PlayListBean = getBean()
        mPlayListBean.mIds.clear()
        mPlayListBean.mData.clear()
        saveBean(mPlayListBean)
    }

    //删除第一个并返回第二个
    fun deleteFirstAndGetNext(item: LocalMediaListItemBean): LocalMediaListItemBean?{
        val mPlayListBean:PlayListBean = getBean()
        //
        val id  = item.idOfChapter()
        //
        mPlayListBean.mData.removeAt(0)
        mPlayListBean.mIds.remove(id)
        //
        var next: LocalMediaListItemBean? = null
        if (mPlayListBean.mData.isNotEmpty()) {
            next = mPlayListBean.mData.first()
        }
        //
        saveBean(mPlayListBean)
        return next
    }

    private val isDebug = false

    private fun getBean():PlayListBean {
        var bean: PlayListBean
        try {
            bean = mmkv.decodeParcelable(
                    KukooKeyValueManager.KEY_PLAYLIST,
                    PlayListBean::class.java) ?: PlayListBean(arrayListOf(), hashSetOf())
        } catch (e: Throwable){
            LoggerManager.e(TAG,"数据异常")
            e.printStackTrace()
            bean = PlayListBean(arrayListOf(), hashSetOf())
            saveBean(bean)
        }
        if (isDebug) {
            LoggerManager.json(TAG, Gson().toJson(bean))
        }
        if(bean.mData.size != bean.mIds.size){
            //数据校验
            LoggerManager.e(TAG,"数据异常, bean.mData.size != bean.mIds.size")
            bean = PlayListBean(arrayListOf(), hashSetOf())
            saveBean(bean)
        }
        return bean
    }

    private fun saveBean(bean :PlayListBean){
        if (isDebug) {
            LoggerManager.json(TAG, Gson().toJson(bean))
        }
        mmkv.encode(KukooKeyValueManager.KEY_PLAYLIST, bean)
    }

    fun get():ArrayList<LocalMediaListItemBean> {
        return getBean().mData
    }

}